#!/usr/bin/env python
#coding:utf-8
import  pymysql,time,requests
import insert_app01_user_num
date = time.strftime('%Y-%m-%d')
time.strptime(date, '%Y-%m-%d')
j=time.mktime(time.strptime(date,'%Y-%m-%d'))
z=float(j)-86400

ltime=time.localtime(z)
timeStr = time.strftime("%Y%m%d", ltime)
datestrftime=time.strftime('%Y%m%d',ltime)
qiandate=time.strftime('%Y-%m-%d',ltime)
#print(datestrftime)
def select_mysql(sql):
    conn = pymysql.connect(host='121.201.72.168',port=56033, user='daxmall_r', passwd='2hGUnCKoEuvkWNgRbvwo', db='daxmall',
                           charset='UTF8')

    cur = conn.cursor()
    reCount = cur.execute(sql)

    nRet = cur.fetchall()
    cur.close()

    conn.close()
    return nRet

def insert_mysql(sql):
    conn = pymysql.connect(host='121.201.68.21', port=3307, user='jiang', passwd='jiangwenhui', db='daxiangzhanshi',

                           charset='UTF8')

    cur = conn.cursor()

    cur.execute(sql)
    nRet = cur.fetchall()
    conn.commit()

    cur.close()

    conn.close()
    return nRet

ret = requests.get('https://ssl.healthmall.cn/monitor/health/check_uv_service.do?token=8f74a15112e0aa42f57822ccf31d7236&date=%s'%datestrftime)
uv=ret.text

zc=insert_app01_user_num.select_mysql("SELECT COUNT(1) FROM hm_member_user mur WHERE mur.create_time < unix_timestamp(curdate()) and mur.create_time>=unix_timestamp('%s') "%(qiandate))[0][0]
pv=insert_mysql("select sum(number) from app01_the_number_of_clicks where updatetime>='%s'"%(qiandate))[0][0]
ip=insert_mysql("select sum(number) from app01_source_ip where updatetime>='%s'"%qiandate)[0][0]
#print(zc,pv,uv)
order_num_and_zhifurmb=select_mysql("SELECT COUNT(1) AS 订单数量,SUM(s.product_nums) AS 商品数量,SUM(s.actual_total) as 订单总金额 FROM ls_sub s INNER JOIN ls_usr_addr_sub a ON a.`addr_order_sn` = s.`addr_order_sn` INNER JOIN ls_usr_addr r ON r.`addr_id` = a.`addr_id` WHERE s.pay_date >= '%s' and s.pay_date < curdate() and s.status > 1 AND s. STATUS != 5 "%qiandate)

if __name__=="__main__":
    insert_sql="insert into app01_zcpvuv values (null,'%s','%s','%s','%s',curdate(),'%s','%s','%s')"%(zc,pv,uv,qiandate,ip,order_num_and_zhifurmb[0][0],order_num_and_zhifurmb[0][2])
    insert_mysql(insert_sql)
    print(insert_sql)
